function retval = read_from_file (filename)
retval = {};
fid = fopen(filename, "r");
while (-1 ~= (line = fgetl(fid)))
	match = regexp(line, 'C');
	if(match > 0)
		info = strsplit(line);
		nh = regexp(info{1,2}, 'h(\d+)', 'tokens'){1,1}{1,1};	#get height of the pic
		nw = regexp(info{1,3}, 'w(\d+)', 'tokens'){1,1}{1,1};	#get width of the pic	
		
		data = [];
		for k = 1:str2num(nh)
			line = fgetl(fid);
			data = [data;line];
		end
		data;
		retval{length(retval)+1}= data;
	end
end
fclose(fid);
endfunction

function black_area = compute_black_area (a_image)
	black_area = 0;
	[nr, nc] = size(a_image);
	for i = 1:nr
		for j = 1:nc
			if (a_image(i,j) == 'x')
				black_area++;
			end
		end
	end
endfunction

#read both files and store images into two cells, each containing 100 images, i.e., char matrices.
cdata = read_from_file("HW1_c.txt");
edata = read_from_file("HW1_e.txt");

#compute the black area of each c image
c_black_area = [];
for i = 1:length(cdata)
	c_black_area(i) = compute_black_area(cdata{i});
end
c_black_area;

#compute the black area of each e image
e_black_area = [];
for i = 1:length(edata)
	e_black_area(i) = compute_black_area(edata{i});
end
e_black_area;

cedges = [min(c_black_area):max(c_black_area)];
eedges = [min(e_black_area):max(e_black_area)];

chist = histc(c_black_area, cedges);
ehist = histc(e_black_area, eedges);

#actual plot histgrams
figure;
subplot(2,1,1);
bar(cedges,chist,'histc');
axis([min(cedges(1), eedges(1)) max(cedges(length(cedges)), eedges(length(eedges))) 0 max(max(chist), max(ehist))+1]);
subplot(2,1,2);
bar(eedges,ehist,'histc');
axis([min(cedges(1), eedges(1)) max(cedges(length(cedges)), eedges(length(eedges))) 0 max(max(chist), max(ehist))+1]);

#try to find a threshold T that minimizes the cost when applying 
#"if (black-area <= T) decide 'c'; else decide 'e'" as a pattern of classifier
Tstar1 = 0;
Mincost1 = intmax;
n_misC1 = 0;
n_misE1 = 0;
for T = 1:max(max(c_black_area),max(e_black_area))
	costc = 0;
	coste = 0;
	for i = 1:length(c_black_area)
		if (c_black_area(i) > T)
			costc++;
		end
	end
	for i = 1:length(e_black_area)
		if (e_black_area(i) <= T)
			coste++;
		end
	end

	if (costc + coste < Mincost1)
		Mincost1 = costc + coste;
		Tstar1 = T;
		n_misC1 = costc;
		n_misE1 = coste;
	end
end

#try to find a threshold T that minimizes the cost when applying 
#"if (black-area >= T) decide 'c'; else decide 'e'" as a pattern of classifier
Tstar2 = 0;
Mincost2 = intmax;
n_misC2 = 0;
n_misE2 = 0;
for T = 1:max(max(c_black_area),max(e_black_area))
	costc = 0;
	coste = 0;
	for i = 1:length(c_black_area)
		if (c_black_area(i) < T)
			costc++;
		end
	end
	for i = 1:length(e_black_area)
		if (e_black_area(i) >= T)
			coste++;
		end
	end
	if (costc + coste < Mincost2)
		Mincost2 = costc + coste;
		Tstar2 = T;
		n_misC2 = costc;
		n_misE2 = coste;
	end
end

#select the minimum cost from two candidates
if (Mincost1 < Mincost2)
	printf("The best decision rule found: if (black-area <= %i) decide 'c'; else decide 'e'\n", Tstar1);
	printf("A: the threshold = %i\n", Tstar1);
	printf("B: the total number of 'c's misclassified: %i\n", n_misC1);
	printf("C: the total number of 'e's misclassified: %i\n", n_misE1);
else
	printf("The best decision rule found: if (black-area >= %i) decide 'c'; else decide 'e'\n", Tstar2);
	printf("A: the threshold = %i\n", Tstar2);
	printf("B: the total number of 'c's misclassified: %i\n", n_misC2);
	printf("C: the total number of 'e's misclassified: %i\n", n_misE2);
end
